define(function (require, exports, module) {
    var $ = require("jquery");
    var angular = require("angular");
    require('authorize');
    require("permission");
    require("angular-route");
    require("angular-ui");
    require("angular-interceptor");
    require("plupload");
    require('../../myservice/common/nav');
    var util = require("util");
    require("./orders-service");
    require("./base-service");

    var app = angular.module('app', ['ngRoute', 'ngUI', 'ngInterceptor', 'raiseFinanceOrdersModule', 'myNav', 'raiseFinanceBaseModule']);

    app.config(['$routeProvider', function ($routeProvider) {
        $routeProvider
            .when('/list', {templateUrl: 'orders/list.html', controller: 'RaiseFinanceOrdersListController'})
            .when('/edit', {templateUrl: 'orders/edit.html', controller: 'RaiseFinanceOrdersEditController'})
            .otherwise({redirectTo: '/list'});
    }]);

    app.filter(
        'to_trusted', ['$sce', function ($sce) {
            return function (text) {
                return $sce.trustAsHtml(text);
            }
        }]
    )

    app.filter('formatStatus', function () {
        return function (record) {
            var status = record.status;
            var substatus = record.sub_status;
            switch(parseInt(substatus)){
                case 1:
                    return '已过期';
                case 2:
                    return '已取消';
                default:
                    switch (parseInt(status)) {
                        case 0:
                            return '待付款';
                        case 1:
                            return '待解筹';
                        case 2:
                            return '已解筹';
                        case -1:
                            return '无效';
                    };
                    break;
            }

            return '待付款';
        };
    })

    app.controller('RaiseFinanceOrdersListController', ['$scope', '$routeParams', 'raiseFinanceBaseData', 'raiseFinanceOrders', function ($scope, $routeParams, raiseFinanceBaseData, raiseFinanceOrders) {
        $scope.baseData = raiseFinanceBaseData;
        $scope.ordersSvr = raiseFinanceOrders;
        setTimeout(function () {
            // setTimeout 为了让路由先完成，防止 dom 元素闪动
            $scope.$apply(function () {
                raiseFinanceBaseData.initData().then(function () {
                    $scope.filter.buildingId = $scope.baseData.projectId;
                    $scope.filter.apply();
                });
            });
        }, 10);
        /*setTimeout(function () {
         $scope.ordersSvr.query(1, 10, '', $scope.status, 0, $scope.keyword);
         }, 0)*/

        $scope.projectChanged = function () {
            $scope.filter.buildingId = $scope.baseData.projectId;
            if ($scope.baseData.projectId) {
                $scope.filter.apply();
            }
        }

        $scope.filter = {
            status: '',
            period: 0,
            buildingId: '',
            keyword: '',
            pageIndex: 1,
            pageSize: 10,
            apply: function () {
                $scope.ordersSvr.query(this.pageIndex, this.pageSize, this.buildingId, this.status, this.period, this.keyword);
            },
            goto: function (page) {
                this.pageIndex = page;
                this.apply();
            }
        };
        $scope.filterDataStatus = [
            {value: '', name: '全部', selected: true},
            {value: '0', name: '待付款'},
            {value: '1', name: '待解筹'},
            {value: '2', name: '已解筹'},
            {value: '-1', name: '无效'}
        ];
        $scope.filterDataPeriod = [
            {value: '', name: '全部', selected: true},
            {value: '7', name: '最近一周'},
            {value: '30', name: '最近一个月'},
            {value: '90', name: '最近三个月'}
        ];

        $scope.changePeriod = function (item) {
            $scope.filter.period = item.value;
            $scope.filter.apply();
        };
        $scope.changeStatus = function (item) {
            $scope.filter.status = item.value;
            $scope.filter.apply();
        };

    }])

    app.controller('RaiseFinanceOrdersEditController', ['$scope', '$routeParams', function ($scope, $routeParams) {

    }])

    angular.bootstrap(document, ['app']);
});